<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;

use App\Models\WebSite;
use App\Models\WebSiteClass;
use App\Models\Admin;
use App\Models\SearchKey;
use App\Models\Sessions;

class AdminController extends Controller
{
    // 后台登录
    public function login()
    {
        // return Hash::make('rootweb123');
        return view('admin.login');
    }

    public function postLogin(Request $request)
    {
        $username = $request->username;
        $password = $request->password;        

        $adminUser = Admin::where('username', $username)->first();

        if(!$adminUser)
        {
            return ['code' => 0, 'msg' => '用户名不存在'];
        }

        // 验证密码是否正确
        if (Hash::check($password, $adminUser->password))
        {
            // 登录成功
            Session::put('username', $username);
            // 跳转到后台首页
            // return redirect('admin/index');

            return ['code' => 1, 'msg' => '登录成功'];
        } else {
            return ['code' => 0, 'msg' => '密码错误'];
        }
    }

    // 退出登录
    public function logout()
    {
        // 清空指定的值
        Session::forget('username');
        return redirect('admin/login');
    }

    // 后台首页
    public function index()
    {
        return view('admin.index');
    }

    // 主控面板
    public function main()
    {
        return view('admin.main');
    }

    // 网站列表
    public function website(Request $request)
    {
        $search = $request->input('search');

        if($search == '')
        {
            // 显示全部的网站列表
            $websites = WebSite::orderBy('id', 'desc')->paginate(20);
            return view('admin.website', array(
                'websites' => $websites,
                'search' => $search
            ));
        } else {
            // 显示搜索的内容的列表
            $websites = WebSite::where('web_name', 'like' , '%'.$search.'%')->orderBy('id', 'desc')->paginate(20);
            return view('admin.website', array(
                'websites' => $websites,
                'search' => $search
            ));
        }

        
    }

    // 添加网站页面
    public function add_website($id = null)
    {
        $fenleis = WebSiteClass::get();
        $website = WebSite::find($id);

        return view('admin.add_website', array(
            'fenleis' => $fenleis,
            'website' => $website
        ));
    }

    // 保存和添加添加的网站
    public function save_website(Request $request, $id = null)
    {
        $webname = $request->input('webname');
        $fenlei = $request->input('fenlei');

        $file = $request->file('one_pic');        

         if(!$id)
        {
            // 新添加
            $website = new WebSite();
            if(!$file)
            {
                return "请选择上传的图片";
            }
            $filename = 'site' . time() . '.' . $file->extension();
            $path = $file->move('websitepic', $filename); 
        }
        else {       
            // 编辑
            $website = WebSite::find($id);
            if($file)
            {
                // 如果选择了图片
                $filename = $website->pic_url;
                unlink($filename); // 删除文件

                $filename = 'site' . time() . '.' . $file->extension();
                $path = $file->move('websitepic', $filename); 
            }
        }        
        
        $website->web_name  = $webname;
        $website->class_id  = $fenlei;
        if($file)
        {
            $website->pic_url  = 'websitepic/' . $filename;
        }        
        $website->web_url  = $request->input('web_url');
        $website->web_url_cn  = $request->input('web_url_cn');
        $website->github = $request->input('github');
        $website->gitee  = $request->input('gitee');
        $website->desc  = $request->input('desc');
        $website->info  = $request->input('editor');
        $website->add_time  = time();

        $website->save();

        return "保存成功";
    }

    // 删除添加的网站
    public function del_website($id)
    {
        $website = WebSite::find($id);
        // 删除文件
        unlink($website->pic_url);
        // 删除本条数据
        $website->delete();

        return "删除成功";
    }

    // 网站分类列表
    public function webclass()
    {
        $webclass = WebSiteClass::orderBy('add_time', 'desc')->paginate(20);
        return view('admin.webclass', array(
            'webclass' => $webclass
        ));
    }

    // 添加和编辑网站分类
    public function add_webclass($id = null)
    {
        $webclass = WebSiteClass::find($id);
        return view('admin.add_webclass', array(
            'webclass' => $webclass
        ));
    }

    // 保存添加和编辑网站页面
    public function save_webclass(Request $request, $id = null)
    {
        $classname = $request->input('classname');

        if($classname == '')
        {
            return '请输入分类名字';
        }

        if($id == null)
        {
            $webclass = new WebSiteClass();
            $webclass->class_name = $classname;
            $webclass->add_time = time();
            $webclass->save();
            return '添加成功';
        }

        if($id)
        {
            $webclass = WebSiteClass::find($id);
            $webclass->class_name = $classname;
            $webclass->add_time = time();
            $webclass->save();
            return '添加成功';
        }
    }

    // 删除网站分类
    public function del_webclass($id)
    {
        $webclass = WebSiteClass::find($id);
        // 删除本条数据
        $webclass->delete();

        return "删除成功";
    }

    // 搜索关键词列表记录
    public function search_key_list()
    {
        $searchKeys = SearchKey::OrderBy('id', 'DESC')->paginate(20);
        return view('admin.search_key_list', array('searchKeys' => $searchKeys));
    }

    // session列表
    public function sessions_list()
    {
        $sessions = Sessions::OrderBy('last_activity', 'DESC')->paginate(20);
        return view('admin.sessions_list', array('sessions' => $sessions));
    }
}
